<?php
class item_openclosetopic extends template_item
{
	var $default_name = "Open/Close a Topic";
	var $default_description = "Use this item to close an open topic or open a closed topic.";
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['no_forum'] = array();
			$this->item_config['limit'] = 1;
		}
	}
	
	function admin_form()
	{
		$this->ipsclass->forums->forums_init();
		
		require_once(ROOT_PATH.'sources/lib/admin_forum_functions.php');
		
		$forumfunc = new admin_forum_functions();
		$forumfunc->ipsclass =& $this->ipsclass;
		$forums = $forumfunc->ad_forums_forum_list(1);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Which forum this item cannot be applied to?</b>",
															$this->ipsclass->adskin->form_multiselect("item[".$this->item['item_id']."][forum][]", $forums, $this->item_config['no_forum']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Limit this item to the member's own topics?</b>",
															$this->ipsclass->adskin->form_yes_no("item[".$this->item['item_id']."][limit]", $this->item_config['limit']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['no_forum'] = $input['no_forum'] ? $input['no_forum'] : array();
		$save['limit'] = intval($input['limit']) & 1;
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['open_close_a_topic']}</div>
<form action='{$this->base_url}' method='post' id='changeform'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['topic_url']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='input' name='topic' value='' size='30' class='forminput' />
			</td>
		</tr>
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['open_close_topic']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
EOF;
	}
	
	function input_done()
	{
		return isset($this->ipsclass->input['topic']) && isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$topic_id = 0;
		if(is_numeric($this->ipsclass->input['topic']))
		{
			$topic_id = intval($this->ipsclass->input['topic']);
		}
		else
		{
			$match = array();
			preg_match("#showtopic=([0-9]+)#",$this->ipsclass->input['topic'],$match);
			$topic_id = $match[1];
		}
		if(!$topic = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'topics','where'=>"tid=".$topic_id)))
		{
			$this->ipsclass->Error(array('MSG'=>'topic_not_found'));
		}
        $forum = $this->ipsclass->forums->forum_by_id[$topic['forum_id']];
        if ( ! $forum['id'] || ! $topic['tid'] )
        {
        	$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'topic_not_found') );
        }
        if ( ! $this->ipsclass->can_queue_posts($this->forum['id']) )
        {
			if ($topic['approved'] != 1)
			{
				$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'topic_not_found') );
			}
        }
        $this->ipsclass->forums->forums_check_access( $forum['id'], 1, 'topic' );
		if(in_array($topic['forum_id'],$this->item_config['no_forum']))
		{
			$this->ipsclass->Error(array('MSG'=>'topic_not_found'));
		}
		if($this->item_config['limit'] && $topic['starter_id'] != $this->ipsclass->member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'topic_not_own'));
		}
		
		
		$this->ipsclass->DB->do_update('topics',array(
														'state' => $topic['state'] == 'open' ? 'closed' : 'open'
										),"tid=".$topic_id);
		
		$this->ipsclass->lang['topic_state_changed'] = str_replace("<#TOPIC#>",$topic['title'],$this->ipsclass->lang['topic_state_changed']);
		
		$this->delete_items($inventory_ids);
		
		$inv_id = reset($inventory_ids);
		$this->_write_log($inv_id, ($topic['state'] == 'open' ? 'Closed' : 'Open')." topic <a href='{$this->ipsclass->base_url}showtopic={$topic['tid']}'>{$topic['title']}</a>");
		
		$this->ipsclass->market_class->redirect('topic_state_changed',$this->ipsclass->base_url."showtopic=".$topic['tid'],array('url'=>1));
	}
}
?>